package com.autologistics.socket.service;

import com.autologistics.dao.DisparterDao;
import com.autologistics.model.Disparter;
import com.autologistics.service.DisparterService;
import com.autologistics.socket.vo.AuthenticationRes;

import net.sf.json.JSONObject;

public class IDAuthenticationServices {
	
	private JSONObject IDAuthenticationJSON;
	


	public JSONObject getIDAuthenticationJSON() {
		return IDAuthenticationJSON;
	}


	public void setIDAuthenticationJSON(JSONObject iDAuthenticationJSON) {
		IDAuthenticationJSON = iDAuthenticationJSON;
	}



	public IDAuthenticationServices(JSONObject iDAuthenticationJSON) {
		IDAuthenticationJSON = iDAuthenticationJSON;
	}


	public JSONObject IDAuthenticatin(){
		 DisparterDao disparterDao;
		 AuthenticationRes response = new AuthenticationRes();
		 int  idCardNum = IDAuthenticationJSON.optInt("idCardNum", -1);
		
		 if(SpringContextWait.beanFcBeanFactory == null){
				response.setResponseCode(4);
				response.setResponseDescription("system is starting ,please wait for a moment");
				return JSONObject.fromObject(response);
		 }
		 disparterDao = (DisparterDao) SpringContextWait.beanFcBeanFactory.getBean("disparterDao");
		
		
		if(idCardNum == -1){
			response.setResponseCode(-1);
			response.setResponseDescription("can`t recived IDNumber parameter");
			return JSONObject.fromObject(response);
		}
		
		//query database 
		Disparter disparter = disparterDao.getByCardNumber(idCardNum);
		
		if(disparter == null){
			response.setResponseCode(3);
			response.setResponseDescription("user not exist");
		}else if(disparter.getIdCardNum() == null){
			response.setResponseCode(2);
			response.setResponseDescription("server error,cardNum could not be null,please manager check it");
		}
		else{
			response.setResponseCode(0);
			response.setIdCardNum(disparter.getIdCardNum());
			response.setUserName(disparter.getName());
			response.setResponseDescription("sucess");
		}
			
		//response.setResponseDescription(responseDescription);*/
		return JSONObject.fromObject(response);
	}
}
